package com.hzspace.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @Author: LiuHao
 * @Descirption: 代替web.xml配置
 * @Date: 2020/2/25_19:51
 */
public class WebInit implements WebApplicationInitializer {
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.setServletContext(servletContext);
        ctx.register(SpringMvcConfig.class);//加载SpringMvc配置
        ctx.register(SpringConfig.class);//加载SpringConfig配置
        //创建并配置DispatcherServlet
        ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
        dispatcherServlet.addMapping("/");
        dispatcherServlet.setLoadOnStartup(1);

    }
}
